/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-06-28 14:25:40
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-07-07 18:52:19
 * @FilePath: \A06\Device\dev.h
 * @Description: 外设管理
 */

#ifndef __DEV_H_
#define __DEV_H_

#include "dpm32m0xx_libopt.h"
#include "log.h"

/* **************************GPIO************************** */
/*KEY*/
#define KEYR_PORT                       GPIOD
#define KEYR_PIN                        GPIO_PIN_13
#define Get_KeyR()                      GPIO_ReadInputBit(KEYR_PORT , KEYR_PIN)

#define KEYL_PORT                       GPIOD
#define KEYL_PIN                        GPIO_PIN_12
#define Get_KeyL()                      GPIO_ReadInputBit(KEYL_PORT , KEYL_PIN)

/*CHARGE EN OR DIS*/
#define CHARGE_L_PORT                   GPIOA
#define CHARGE_L_PIN                    GPIO_PIN_4
#define EN_CHARGE_L()                   GPIO_SetBit(CHARGE_L_PORT , CHARGE_L_PIN)
#define DIS_CHARGE_L()                  GPIO_ResetBit(CHARGE_L_PORT , CHARGE_L_PIN)

#define CHARGE_R_PORT                   GPIOB
#define CHARGE_R_PIN                    GPIO_PIN_1
#define EN_CHARGE_R()                   GPIO_SetBit(CHARGE_R_PORT , CHARGE_R_PIN)
#define DIS_CHARGE_R()                  GPIO_ResetBit(CHARGE_R_PORT , CHARGE_R_PIN)

/*display:software i2c communication*/
#define DISPLAY_SCL_PORT                GPIOA
#define DISPLAY_SCL_PIN                 GPIO_PIN_9
#define DISPLAY_SCL_HIGH                GPIO_SetBit(DISPLAY_SCL_PORT , DISPLAY_SCL_PIN)
#define DISPLAY_SCL_LOW                 GPIO_ResetBit(DISPLAY_SCL_PORT , DISPLAY_SCL_PIN)


#define DISPLAY_SDA_PORT                GPIOA
#define DISPLAY_SDA_PIN                 GPIO_PIN_10
#define DISPLAY_SDA_HIGH                GPIO_SetBit(DISPLAY_SDA_PORT , DISPLAY_SDA_PIN)
#define DISPLAY_SDA_LOW                 GPIO_ResetBit(DISPLAY_SDA_PORT , DISPLAY_SDA_PIN)



/* **************************EPWM************************** */
#define L_BAT_PWM_PORT                  GPIOD
#define L_BAT_PWM_PIN                   GPIO_PIN_0
#define L_BAT_EPWM_AF                   GPIO_ALT_FUNC_2
#define L_BAT_EPWM_CHANNEL              EPWM_CH0


#define R_BAT_PWM_PORT                  GPIOA
#define R_BAT_PWM_PIN                   GPIO_PIN_8
#define R_BAT_EPWM_AF                   GPIO_ALT_FUNC_5
#define R_BAT_EPWM_CHANNEL              EPWM_CH2

#include "systick.h"
#define EPWM_Fre                        (90000UL)       /*EPWM频率 单位:Hz*/
#define EPWM_PERIOD                     ((SYS_Frequency / EPWM_Fre) - 1)        /*epwm重装载值*/
#define EPWM_DUTY(x)                    (((x) * EPWM_PERIOD) / 100)
#define EPWM_DUTY_1000(x)               (((x) * EPWM_PERIOD) / 1000)

#define EPWM_MAX_DUTY                    EPWM_DUTY(47)
#define EPWM_MIN_DUTY                    EPWM_DUTY(1)


/* **************************ADC************************** */
/*
*   BAT_L_CUR       0
*   BAT_R_CUR       1
*   BAT_L_VOL       6
*   BAT_R_VOL       5
*   TEMP            4
*   C+              7
*/
#define BAT_L_CUR_CHANNEL       ADC_Channel_0
#define BAT_R_CUR_CHANNEL       ADC_Channel_1

#define BAT_L_VOL_CHANNEL       ADC_Channel_6
#define BAT_R_VOL_CHANNEL       ADC_Channel_7

#define PCB_TEMP_CHANNEL        ADC_Channel_12

#define VIN_CHANNEL             ADC_Channel_10
#define ALL_ADC_CHANNEL         (BAT_L_CUR_CHANNEL | BAT_R_CUR_CHANNEL | BAT_L_VOL_CHANNEL | \
                                BAT_R_VOL_CHANNEL | PCB_TEMP_CHANNEL | VIN_CHANNEL)

/*adc port*/
#define BAT_L_VOL_PORT          GPIOA
#define BAT_L_VOL_PIN           GPIO_PIN_6

#define BAT_R_VOL_PORT          GPIOA
#define BAT_R_VOL_PIN           GPIO_PIN_7

#define PCB_TEMP_PORT           GPIOD
#define PCB_TEMP_PIN            GPIO_PIN_1

#define VIN_PORT                GPIOD
#define VIN_PIN                 GPIO_PIN_8

/* **************************PGA************************** */
#define LBAT_CUR_PGAP_PORT      GPIOA
#define LBAT_CUR_PGAP_PIN       GPIO_PIN_0

#define LBAT_CUR_PGAN_PORT      GPIOA
#define LBAT_CUR_PGAN_PIN       GPIO_PIN_1


#define RBAT_CUR_PGAP_PORT      GPIOA
#define RBAT_CUR_PGAP_PIN       GPIO_PIN_2

#define RBAT_CUR_PGAN_PORT      GPIOA
#define RBAT_CUR_PGAN_PIN       GPIO_PIN_3

void dev_init(void);

void gpio_config(void);
void epwm_config(void);
void adc_config(void);
void PGA_config(void);


extern uint8_t gDisplay_refresh;

#endif

